class Solution(object):
    def getMinSwaps(self, num, k):
        c = 0
        n = len(num)
        ori = num
        while c < k:
            index = n - 2
            while num[index] >= num[index + 1]:
                index -= 1
            right_index = index + 1
            while right_index < n:
                if num[right_index] > num[index]:
                    right_index += 1
                else:
                    break
            c += 1
            num = num[:index] + num[right_index - 1] + (num[index + 1: right_index - 1] + num[index] + num[right_index:])[::-1]
        left = 0
        right = 0
        ans = 0
        while left < n and right < n:
            if ori[left] == num[right]:
                left += 1
                right += 1
            else:
                while num[right] != ori[left]:
                    left += 1
                ans += left - right
                ori = ori[:right] + ori[left] + ori[right: left] + ori[left + 1:]
                right += 1
                left = right
        return ans

data = Solution()
num = "11112"
k = 4
print(data.getMinSwaps(num, k))